/**
 * 
 */
package com.jm.web.data;

import java.util.Vector;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.jm.utils.StringHelper;

/**
 * @author yanjie
 *
 * 2011/10/10 上午10:15:25
 */
public class PageResult extends AjaxActionResult{
	protected ReturnData retData = null ;
	protected Vector<FormItemError> formItemErrors = null;
	
	/**
	 * 註冊錯誤消息
	 * @param strFormItemId
	 * @param nErrorType
	 * @param strErrorInfo
	 */
	public void Register(String strFormItemId,String strErrorInfo)
	{
		if(formItemErrors == null)
			formItemErrors = new Vector<FormItemError>();
		
		FormItemError formItemError = FindFormItemError(strFormItemId,true);
		formItemError.setErrorInfo(strErrorInfo);
		formItemErrors.add(formItemError);
		
		if(this.IsError() && this.getRetCode() == Errors.OK)
			this.setRetCode(Errors.NOK);
	}
	
	/**
	 * 查找錯誤項
	 * @param strFormItemId
	 * @param bNew
	 * @return
	 */
	protected FormItemError FindFormItemError(String strFormItemId,boolean bNew)
	{
		if(formItemErrors != null)
		{
			int nCount = formItemErrors.size();
			for(int i=0;i<nCount;i++)
			{
				FormItemError formItemError  = (FormItemError)formItemErrors.get(i);
				if(StringHelper.Compare(strFormItemId, formItemError.getFormItemId(), true) == 0)
				{
					return formItemError;
				}
			}
		}
		
		
		if(!bNew)
			return null;
		
		FormItemError formItemError = new FormItemError();
		formItemError.setFormItemId(strFormItemId);
		return formItemError;
	}
	
	 /**
     * 判斷結果是否為錯誤
     * @return
     */
	@Override
    public boolean IsError()
    {
    	if(nRetCode==Errors.NOK)
    		return true ;
    	if(formItemErrors != null && formItemErrors.size() > 0)
    		return true ;
    	
    	return false;
    }
	
	
	
	@Override
	protected void FillJSONObject(JSONObject objJSON) {
		super.FillJSONObject(objJSON);
		
		if(!IsError() && formItemErrors != null && formItemErrors.size() > 0){
			this.setRetCode(Errors.NOK);
		}
		JSONArray json = JSONArray.fromCollection(formItemErrors);
		objJSON.put("errors", json);
		
		if(this.getData() != null){
			if(this.getData().ContainesParam("loadingcancelparams") && this.getLoadingCancelParams().isNullOrEmpty()){
				this.getData().RemoveParam("loadingcancelparams");
			}
			
			if(this.getData().ContainesParam("loadingsaveparams") && this.getLoadingSaveParams().isNullOrEmpty()){
				this.getData().RemoveParam("loadingsaveparams");
			}
			
			objJSON.put("data", this.getData().ToJSONString());
		}
	}

	public ReturnData getData() {
		return retData;
	}
	
	public void setRetData(ReturnData retData) {
		this.retData = retData;
	}
	
	/**
	 * @return 返回取消按钮的参数
	 */
	public BaseDataEntity getLoadingCancelParams(){
		if(!this.getData().ContainesParam("loadingcancelparams")){
			BaseDataEntity calparams = new BaseDataEntity();
			this.getData().SetParamValue("loadingcancelparams",calparams);
		}
		return (BaseDataEntity)this.getData().GetParamValue("loadingcancelparams");
	}
	
	/**
	 * @return 返回保存按钮的参数
	 */
	public BaseDataEntity getLoadingSaveParams(){
		if(!this.getData().ContainesParam("loadingsaveparams")){
			BaseDataEntity calparams = new BaseDataEntity();
			this.getData().SetParamValue("loadingsaveparams",calparams);
		}
		return (BaseDataEntity)this.getData().GetParamValue("loadingsaveparams");
	}
	
	/**
	 * @return 返回更新后的记录
	 */
	public BaseDataEntity getLoadingrecord(){
		if(!this.getData().ContainesParam("loadingrecord")){
			BaseDataEntity calparams = new BaseDataEntity();
			this.getData().SetParamValue("loadingrecord",calparams);
		}
		return (BaseDataEntity)this.getData().GetParamValue("loadingrecord");
	}
}
